public partial class Wages : Form
    {
        public Wages()
        {
            InitializeComponent();
        }

        private void Wages_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'stafScheDataSet.BaseWage' table. You can move, or remove it, as needed.
            this.baseWageTableAdapter.Fill(this.stafScheDataSet.BaseWage);
            // TODO: This line of code loads data into the 'stafScheDataSet.vw_atte' table. You can move, or remove it, as needed.
            this.vw_atteTableAdapter.Fill(this.stafScheDataSet.vw_atte);
            // TODO: This line of code loads data into the 'stafScheDataSet.Staff' table. You can move, or remove it, as needed.
            this.staffTableAdapter.Fill(this.stafScheDataSet.Staff);
            // TODO: This line of code loads data into the 'stafScheDataSet.Schedule' table. You can move, or remove it, as needed.
            this.scheduleTableAdapter.Fill(this.stafScheDataSet.Schedule);
            txtYear.Text = DateTime.Now.Year.ToString();
            cbMonth.Text = DateTime.Now.Month.ToString();
            Calc(txtYear.Text, cbMonth.Text);
        }

        void Calc(string year, string month)
        {
            dataGridView1.DataSource = null;
            DataTable dtbl = new DataTable();
            dtbl.Columns.Add("StaffID");
            dtbl.Columns.Add("Arrange");
            dtbl.Columns.Add("Absence");
            dtbl.Columns.Add("Actual");
            dtbl.Columns.Add("Wages");
            try
            {
                string strStartDate = year + "-" + month + "-1";
                DateTime dtStart = DateTime.Parse(strStartDate);
                DateTime dtEnd = dtStart.AddMonths(1);
                DataTable dtSche = stafScheDataSet.Schedule.Select("startTime>='"
                    + strStartDate + "' and startTime<'" + dtEnd.ToString("yyyy-MM-dd")
                    + "'").CopyToDataTable();
                var result = from row in dtSche.AsEnumerable()
                             group row by row.Field<int>("StaffID") into grp
                             select new
                             {
                                 StaffId = grp.Key,
                                 SumHours = grp.Sum((r) => decimal.Parse(r["Hours"].ToString()))
                             };
                foreach (var t in result)
                {
                    DataRow dr = dtbl.NewRow();
                    dr["StaffID"] = t.StaffId;
                    dr["Arrange"] = t.SumHours;
                    dtbl.Rows.Add(dr);
                }
                decimal dBaseWage = (decimal)stafScheDataSet.BaseWage.Select("id=1")[0]["BaseWage"];
                try
                {
                    DataTable dtAtte = stafScheDataSet.vw_atte.Select("startTime>='"
                        + strStartDate + "' and startTime<'" + dtEnd.ToString("yyyy-MM-dd")
                        + "'").CopyToDataTable();
                    result = from row in dtAtte.AsEnumerable()
                             group row by row.Field<int>("StaffID") into grp
                             select new
                             {
                                 StaffId = grp.Key,
                                 SumHours = grp.Sum((r) => decimal.Parse(r["Hours"].ToString()))
                             };
                    foreach (var t in result)
                    {
                        DataRow dr = dtbl.Select("StaffID='" + t.StaffId.ToString() + "'")[0];
                        dr["Absence"] = t.SumHours;
                        dr["Actual"] = decimal.Parse(dr["Arrange"].ToString()) - t.SumHours;
                        dr["Wages"] = (decimal.Parse(dr["Actual"].ToString())
                            * dBaseWage).ToString("0.00");
                        dtbl.AcceptChanges();
                    }
                }
                catch { }
                foreach (DataRow dr in dtbl.Rows)
                {
                    string strName = stafScheDataSet.Staff.Select("id='"
                        + dr["StaffID"].ToString() + "'")[0]["Name"].ToString();
                    dr["StaffID"] = strName;
                    if (string.IsNullOrEmpty(dr["Absence"].ToString()))
                    {
                        dr["Actual"] = dr["Arrange"];
                        dr["Wages"] = (decimal.Parse(dr["Actual"].ToString())
                            * dBaseWage).ToString("0.00");
                    }
                }
            }
            catch { }
            dataGridView1.DataSource = dtbl;
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            Calc(txtYear.Text, cbMonth.Text);
        }
    }
